package com.cn.trip.DbDao;

import com.cn.trip.DaoUtil.DBHelper;
import com.cn.trip.bean.Order;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OrderDao {
	private ResultSet rs;
	private PreparedStatement ps;
	private Connection conn;
	
	public int addOrder(Order o) {
		String sql="insert into orders(orderNum,orderTime,user_id,money) values (?,?,?,?)";
		conn= DBHelper.getConnection();
		try {
			ps=conn.prepareStatement(sql);
			ps.setLong(1, o.getOrder_num());
			ps.setString(2, o.getDate());
			ps.setInt(3, o.getUser_id());
			ps.setFloat(4, o.getMoney());
			return ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
		
	}
	
	public List<Order> getOrder(int user_id){
		String sql="select * from orders where user_id=?";
		conn=DBHelper.getConnection();
		List<Order> list=new ArrayList<>();
		try {
			ps=conn.prepareStatement(sql);
			ps.setInt(1, user_id);
			rs=ps.executeQuery();
			while(rs.next()) {
				Order o=new Order();
				o.setId(rs.getInt(1));
				o.setOrder_num(rs.getLong(2));
				o.setDate(rs.getString(3));
				o.setMoney(rs.getFloat(5));
				list.add(o);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
}
